
package com.example.demo.config.cos;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.region.Region;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * Author: XiaoBingBy
 * Email: XiaoBingBy@qq.com
 * Date: 2017/9/25
 * Time: 13:01
 * Describe: 腾讯云对象存储配置 https://console.qcloud.com/cos
 */

@Configuration
@EnableConfigurationProperties(CosConfigProperties.class)
//@ConditionalOnProperty(name = "enable")
public class CosConfig {

    @Autowired
    private CosConfigProperties cosConfigProperties;

    @Bean
    public COSClient cosClient() {
        // 初始化客户端配置 设置bucket所在的区域，比如广州(gz), 天津(tj)
        ClientConfig clientConfig = new ClientConfig(new Region(cosConfigProperties.getRegion()));
        // 初始化秘钥信息
        COSCredentials   cred = new BasicCOSCredentials(cosConfigProperties.getSecretId(),cosConfigProperties.getSecretKey());
        // 初始化cosClient
        COSClient cosClient = new COSClient(cred, clientConfig);
        return cosClient;
    }

}

